from base.base import Base
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys

class Page_AlarmSet(Base):

        
    topnav_locator = (By.XPATH,'(//li[contains(@class,"ant-menu-overflow-item")])[7]')
    topnav_nav_locator = (By.XPATH,'//span[text()="告警设置"]')
    input_siteseach_locator = (By.CLASS_NAME,'ant-select-selection-search-input')
    site_locator = (By.XPATH,'(//div[@class="ant-cascader-menus"]//li)[1]')
    input_seachdevice_locator = (By.XPATH,'//input[@placeholder="设备搜索"]')
    device_errinfo_locator = (By.XPATH,'(//p[@class="ant-empty-description"])[1]')
    device_locator = (By.XPATH,'//span[text()=" xq测试-通用测温"]')      
    cases_state_locator = (By.XPATH,'//span[@class="ant-switch-inner"]')
    device_name_locator = (By.XPATH,'(//div[@class="ant-card-body"])[2]')
    alarmset_info_locator = (By.XPATH,'//input[@class="ant-input-number-input"]')



    def page_jump(self):
        action = ActionChains(self.driver)
        action.move_to_element(self.find(self.topnav_locator)).perform()
        action.click(self.find(self.topnav_nav_locator)).perform()
    
    def input_site(self,sitename):
        self.input(self.input_siteseach_locator,sitename)

    def click_site(self):
        self.click(self.site_locator)

    def input_device(self,devicename):
        self.input(self.input_seachdevice_locator,devicename)
    
    def get_device_errorinfo(self):
        return self.get_text(self.device_errinfo_locator)
    
    def click_device(self):
        self.click(self.device_locator)

    def get_devicename(self):
        return self.get_text(self.device_name_locator).split('\n')[0]

    def get_alarmset_info(self):
        values = []
        elms = self.finds(self.alarmset_info_locator)
        for elm in elms:
            values.append(int(elm.get_attribute('value')))
        return values
    
    def set_alarmset_info(self,value=50):
        elms = self.finds(self.alarmset_info_locator)
        for elm in elms:
            #模拟删除功能
            elm.send_keys(Keys.CONTROL+'a')
            elm.send_keys(Keys.DELETE)
            elm.send_keys(value)
    
    #如果id=0,返回顶部按钮状态文本，id!=0,返回下方所有按钮状态文本
    def get_alarmsetlist(self,id=0):
        values = []
        elms = self.finds(self.cases_state_locator)
        if id == 0:
            return self.get_text_elm(elms[0])
        else:
            for index,elm in enumerate(elms):
                if index == 0:
                    continue
                else:
                    values.append(self.get_text_elm(elm))
            return values
    
    #id=0,切换顶部设备告警，id!=0,切换下方9个设备
    def switch_btn_alarmset(self,id=0):
        for index,elm in enumerate(self.finds(self.cases_state_locator)):
            if index == 0 and id == 0:
                elm.click()
            elif id != 0 and 0 < index <= 10:
                elm.click()

